package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.common.BaseUserInfo;
import com.api.syh56.shengyunhui.common.UserInfoEnu;
import com.api.syh56.shengyunhui.entity.Address;
import com.api.syh56.shengyunhui.entity.Car;
import com.api.syh56.shengyunhui.exception.BaseException;
import com.api.syh56.shengyunhui.mapper.CarMapper;
import com.api.syh56.shengyunhui.service.CarService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {

    @Autowired
    private CarMapper carMapper;

    @Transactional
    public void setAlltoDefault(Long uid){
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Car::getUid,uid);
        List<Car> lists = list(wrapper);
        lists.forEach(item->{
            item.setIsDefault(false);
        });
        updateBatchById(lists);
    }


    @Override
    public void setDefault(Long id) {
        Car car = carMapper.selectById(id);
        car.setIsDefault(true);
        updateById(car);
    }

    @Transactional
    public void addNewCar(Car car) {
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        car.setUid(uid);
        //新插入地址如果设为默认
        if(car.getIsDefault()){
            setAlltoDefault(uid);
            saveOrUpdate(car);
        } else {
            saveOrUpdate(car);
        }
    }

    @Override
    public void removeCar(Car car) {
        Long id = car.getId();
        Car carDB = carMapper.selectById(id);
        //如果删除的car是默认车辆
        if(carDB.getIsDefault()){
            QueryWrapper<Car> wrapper = new QueryWrapper<>();
            Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
            wrapper.lambda().eq(Car::getUid,uid);
            List<Car> carList = list(wrapper);
            //如果车辆簿还有其他车辆，将其设置为默认，再删除选中车辆
            if(!carList.isEmpty()){
                setDefault(carList.get(0).getId());
            }
        }
        //如果车辆簿没有其他车辆，直接删除
        removeById(id);
    }

    @Transactional
    public void updateCar(Car car) {
        if(car.getIsDefault()){
            Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
            setAlltoDefault(uid);
            saveOrUpdate(car);
        } else {
            saveOrUpdate(car);
        }
    }


    public Car selectById(Long id) {
        Car car = carMapper.selectById(id);
        if (car == null) {
            throw new BaseException("id不存在");
        }
        return car;
    }

    public Page<Car> selectPageByuid(Long uid, Integer pageSize, Integer pageNum) {
        Page<Car> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Car::getUid,uid);
        Page<Car> carPage = carMapper.selectPage(page, wrapper);
        return carPage;
    }
}
